<?php

require("./Conn.php");

//create or replace PROCEDURE PRC001(C1 OUT SYS_REFCURSOR)  AS
//        NOMBRE varchar(30);
//        BEGIN
//			OPEN C1 FOR
//					SELECT a.tablespace_name, a.table_name, a.num_rows, ROUND(SUM (b.bytes)) / 1024 / 1024 Mb
//					FROM (SELECT tablespace_name, table_name, num_rows FROM user_tables) a,
//					(SELECT segment_name, tablespace_name, bytes FROM user_extents) b WHERE a.table_name = b.segment_name
//					AND a.tablespace_name = b.tablespace_name GROUP BY a.tablespace_name, a.table_name,a.num_rows;

//
////END PRC001;
//--/
if (!isset($_POST['req'])) {
    die("No request provided.");
} else if (filter_input(INPUT_POST, 'req') == "Procedureando1") {
        
        $json = array("success" => false, "rowCount" => 0, "Elements" => Array());
        
        
        if (!$conn) {
            
            $m = oci_error();
            trigger_error(htmlentities($m['message']), E_USER_ERROR);
        }
       
        $curs = oci_new_cursor($conn);
        $stid = oci_parse($conn, "begin PRC001(:cursbv); end;");
        oci_bind_by_name($stid, ":cursbv", $curs, -1, OCI_B_CURSOR);
        oci_execute($stid);

        oci_execute($curs);  // Execute the REF CURSOR like a normal statement id 
        
        $i =0;
        while ($row = oci_fetch_array($curs, OCI_ASSOC+OCI_RETURN_NULLS)) {
            array_push($json['Elements'], $row);
            $i++;
        }

        if ($i > 0) {
       
            $json['success'] = true;
            $json['rowCount'] =$i;
        }

        print( json_encode($json));

//        oci_free_statement($stid);
//        oci_free_statement($curs);
//        oci_close($conn);
    }
    //SOLICITUD PARA GUARDAR EL ARCHIVO DE CONFIGURACION
    if (filter_input(INPUT_POST, 'req') == "Configurando") {
      
    $arrayConf = filter_input(INPUT_POST, 'arrayConf');//obtengo el array del javascript
    $ArchivoConf = fopen("conf.json", "w+") or die(json_encode("No .json")); 
    //fwrite($ArchivoConf,filter_input(INPUT_POST, 'arrayConf') );
    fwrite($ArchivoConf,$arrayConf );
    
    
    }
    
    
    //solicita para levantar el archivo de configuracion
    if (filter_input(INPUT_POST, 'req') == "Configurando2") {
      
    
    $myfile = fopen("conf.json", "r") or die("Unable to open file!");
    $arrayconf = fgets($myfile);
    fclose($myfile);
    
    $Conf = array("success" => false, "Elements" => Array());
        
        $aux = json_decode($arrayconf);
        array_push($Conf['Elements'], $aux);
      
            $Conf['success'] = true;
  
        //print( json_encode("lega aqui"));
        print(json_encode($Conf));
    }

      